<?php

require dirname(__FILE__) . 
    DIRECTORY_SEPARATOR . '..' . 
    DIRECTORY_SEPARATOR . 'source' .
    DIRECTORY_SEPARATOR . 'ClassStructure' . 
    DIRECTORY_SEPARATOR . 'UtilityFunctions.php';

require dirname(__FILE__) . 
    DIRECTORY_SEPARATOR . '..' . 
    DIRECTORY_SEPARATOR . 'source' .
    DIRECTORY_SEPARATOR . 'ClassStructure' . 
    DIRECTORY_SEPARATOR . 'Exception.php';
    
require dirname(__FILE__) . 
    DIRECTORY_SEPARATOR . '..' . 
    DIRECTORY_SEPARATOR . 'source' .
    DIRECTORY_SEPARATOR . 'ClassStructure' . 
    DIRECTORY_SEPARATOR . 'PointCut.php';
    
require dirname(__FILE__) . 
    DIRECTORY_SEPARATOR . '..' . 
    DIRECTORY_SEPARATOR . 'source' .
    DIRECTORY_SEPARATOR . 'ClassStructure' . 
    DIRECTORY_SEPARATOR . 'Abstract.php';
    
require dirname(__FILE__) . 
    DIRECTORY_SEPARATOR . '..' . 
    DIRECTORY_SEPARATOR . 'source' .
    DIRECTORY_SEPARATOR . 'ClassStructure' . 
    DIRECTORY_SEPARATOR . 'Delegator.php';
    
require dirname(__FILE__) . 
    DIRECTORY_SEPARATOR . '..' . 
    DIRECTORY_SEPARATOR . 'source' .
    DIRECTORY_SEPARATOR . 'ClassStructure' . 
    DIRECTORY_SEPARATOR . 'Weaved.php';
    
require dirname(__FILE__) . 
    DIRECTORY_SEPARATOR . '..' . 
    DIRECTORY_SEPARATOR . 'source' .
    DIRECTORY_SEPARATOR . 'ClassStructure' . 
    DIRECTORY_SEPARATOR . 'Weaved' . 
    DIRECTORY_SEPARATOR . 'Delegator.php';

require dirname(__FILE__) . 
    DIRECTORY_SEPARATOR . '..' . 
    DIRECTORY_SEPARATOR . 'source' .
    DIRECTORY_SEPARATOR . 'ClassStructure' . 
    DIRECTORY_SEPARATOR . 'Weaved' . 
    DIRECTORY_SEPARATOR . 'Layers.php';

require dirname(__FILE__) . 
    DIRECTORY_SEPARATOR . '..' . 
    DIRECTORY_SEPARATOR . 'source' .
    DIRECTORY_SEPARATOR . 'ClassStructure' . 
    DIRECTORY_SEPARATOR . 'Mixin.php';
    
require dirname(__FILE__) . 
    DIRECTORY_SEPARATOR . '..' . 
    DIRECTORY_SEPARATOR . 'source' .
    DIRECTORY_SEPARATOR . 'ClassStructure' . 
    DIRECTORY_SEPARATOR . 'Mixin' . 
    DIRECTORY_SEPARATOR . 'Delegator.php';
    
require dirname(__FILE__) . 
    DIRECTORY_SEPARATOR . '..' . 
    DIRECTORY_SEPARATOR . 'source' .
    DIRECTORY_SEPARATOR . 'ClassStructure' . 
    DIRECTORY_SEPARATOR . 'Property.php';
    
require dirname(__FILE__) . 
    DIRECTORY_SEPARATOR . '..' . 
    DIRECTORY_SEPARATOR . 'source' .
    DIRECTORY_SEPARATOR . 'ClassStructure' . 
    DIRECTORY_SEPARATOR . 'Mixin' .
    DIRECTORY_SEPARATOR . 'Property.php';
    
require dirname(__FILE__) . 
    DIRECTORY_SEPARATOR . '..' . 
    DIRECTORY_SEPARATOR . 'source' .
    DIRECTORY_SEPARATOR . 'ClassStructure' . 
    DIRECTORY_SEPARATOR . 'Mixin' .
    DIRECTORY_SEPARATOR . 'PhpEnvironment.php';
    
    
set_include_path(implode(PATH_SEPARATOR, array(
    realpath('/home/walter/svn/mission-control/libraries/zend-framework/library'),
    realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'source'),
    get_include_path(),
)));

require_once 'Zend/Loader/Autoloader.php';

$obj_loader = Zend_Loader_Autoloader::getInstance();
$obj_loader->registerNamespace('Common_');
$obj_loader->registerNamespace('Utility_');
  
Common_Template::configure(array(
    'script_path' => array(
        dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'views'
    )
));

/**
 * Uses the PHP debug_backtrace() to provide debugging information
 * 
 * This particular method will print the following pattern of debuging 
 * information:
 * 
 * {file_name} ({line_number}) : object({object_name})->{function_name}({number_of_parameters}) [{class_name}::{function_name}]
 * 
 * {file_name} The full path and file name of where the trace was invoked.
 * {line_number} The line number within the file where the trace was invoked.
 * {object_name} The object's class name in which the trace was invoked.
 * {function_name} The object's method name in which the trac was invoked.
 * {number_of_parameters} The number of parameters supplied to the method.
 * {class_name} The class which provides implementation for this method.
 * 
 * @param mixed @mix Any variable or data to print during this trace
 * @return void
 */
function debug_trace($mix = null) { 
    $arr_debug = debug_backtrace();
    $arr_debug_file = $arr_debug[0];
    $arr_debug_operation = $arr_debug[1];
    
    $str_file = $arr_debug_file['file'].' ('.$arr_debug_file['line'].')';
    
    $str_operation = '';
    if(!is_null($arr_debug[1])) {
        $str_operation = ': object('.get_class($arr_debug[1]['object']).')'.$arr_debug[1]['type'].$arr_debug[1]['function'].'('.count($arr_debug[1]['args']).') ['.$arr_debug[1]['class'].'::'.$arr_debug[1]['function'].']';
    }
    
    $str_debug = $str_file.$str_operation."\n";
    
    if(is_object($mix)) {
        $str_debug .= (method_exists($mix, '__toString')) ? 
            $mix->__toString() :
            get_class($mix);
        $str_debug .= "\n";
    } else if(is_array($mix)) {
        $str_debug .= debug_array($mix)."\n";
    } else {
        $str_debug .= $mix."\n";
    }
    
    if (php_sapi_name() != 'cli') {
    	echo('<pre>'.$str_debug.'</pre>');
    } else {
    	echo($str_debug);
    }
}

/**
 * Returns a string representation of an array structure
 * 
 * @param array $arr The array source to debug.
 * @param string $int_level The debug level, and therefore indent mulitplier.
 * @return string
 */
function debug_array($arr, $int_level = 0) {
    $str = 'array ['.count($arr).'] ('."\n";
    $int_max_length = 0;
    
    foreach($arr as $str_key => $mix_value) {
    	$int_length = strlen($str_key);
    	if($int_length > $int_max_length) {
    		$int_max_length = $int_length;
    	}
    }
    
    foreach($arr as $str_key => $mix_value) {
        $str .= str_repeat(ACD_DEBUG_INDENT, $int_level+1).'['.str_pad($str_key, $int_max_length).'] => ';
        if(!is_array($mix_value)) {
            $str .= debug_datatype($mix_value);
        } else {
            $str .= debug_array($mix_value, $int_level+1);
        }
        $str .= "\n";
    }
    $str .= str_repeat(ACD_DEBUG_INDENT, $int_level).")";
    
    return $str;
}

/**
 * Returns a string representation of any variable, using PHP's print_r()
 * 
 * @param mixed $mix Any variable to debug.
 * @return void
 */
function debug_object($mix) {
	if(php_sapi_name() != 'cli') { echo('<pre>'); }
	print_r($mix);
	if(php_sapi_name() != 'cli') { echo('</pre>'); }
	if(php_sapi_name() == 'cli') { echo("\n"); }
}

function debug_echo($mix) {
	$mix = !is_null($mix) ? $mix : '[n]';
	if(php_sapi_name() == 'cli') {
		echo($mix."\n");
	} else {
		echo('<pre>'); 
		echo($mix);
		echo('</pre>'); 
	}
}

/**
 * Returns a brief string representation of the variable's data type and its value
 * 
 *  n = null
 *  b = boolean
 *  s = string
 *  i = integer
 *  f = float
 *  a = array
 *  o = object
 *  r = resource
 * 
 * @param mixed $mix Any variable to debug.
 * @return void
 */
function debug_datatype($mix) {
  $output = '';
  if(is_null($mix)) {
    $output = '[n]';
  } else if(is_bool($mix)) {
    $mix = ($mix) ? 'true' : 'false';
    $output = '[b] '.$mix;
  } else if(is_string($mix)) {
    $int_length = strlen($mix);
    $output = '[s] length('.$int_length.')';
    if($int_length <= 256) {
    	$output .= ' '.$mix;
    }
  } else if(is_int($mix)) {
    $output = '[i] '.$mix;
  } else if(is_float($mix)) {
    $output = '[f] '.$mix;
  } else if(is_array($mix)) {
    $output = '[a] array('.count($mix).')';
  } else if(is_object($mix)) {
    $output = '[o] object('.get_class($mix).')';
  } else if(is_resource($mix)) {
    $output = '[r] type('.get_resource_type($mix).')';
  }
  return $output;
}

function debug_var($mix) {
	debug_echo(debug_datatype($mix));
}

function eggx($mix, $str_title = null, $int_type = Zend_Log::DEBUG) {
    
    // only log if we have a writer available
    if(!Zend_Registry::isRegistered('firephp_logger')) {
        return;
    }
	
	$obj_logger = Zend_Registry::get('firephp_logger');
	
	if(!empty($str_title)) {
		
		$arr_debug = debug_backtrace();
	
		$str_file = $arr_debug[0]['file'];
		$str_line = $arr_debug[0]['line'];
		$str_operation = '';
		
		//$str_file = substr($str_file, strrpos($str_file, DIRECTORY_SEPARATOR));
		
		if(isset($arr_debug[1])) {
			$str_class = '';
	
			$str_method = $arr_debug[1]['function'];
			$str_type = isset($arr_debug[1]['type']) ? $arr_debug[1]['type'] : '';
			
			if(isset($arr_debug[1]['object'])) {
				$str_class = get_class($arr_debug[1]['class']);
			} else if(isset($arr_debug[1]['class'])) {
				$str_class = $arr_debug[1]['class'];
			}
			
			$str_operation = $str_method . '()';
			if(!empty($str_class)) {
				$str_operation = $str_class . $str_type . $str_operation;
			}
		}
		
		$str_debug =  $str_file . ' [line ' . $str_line . ']';
		if(!empty($str_operation)) {
			$str_debug .= ' within '. $str_operation;
		}
		
		$str_output = '*' . $str_title . '* ' . $str_debug;
		
		$obj_logger->log($str_output, $int_type);
	}
    
    $obj_logger->log($mix, $int_type);
}

function egg($mix, $str_title = 'debug', $int_stack = 0) {
	$arr_debug = debug_backtrace();

	$str_file = $arr_debug[$int_stack]['file'];
	$str_line = $arr_debug[$int_stack]['line'];
	$str_operation = '';
	
	//$str_file = substr($str_file, strrpos($str_file, DIRECTORY_SEPARATOR));
	
    $int_stack++;
    
	if(isset($arr_debug[$int_stack])) {
		$str_class = '';

		$str_method = $arr_debug[$int_stack]['function'];
		$str_type = isset($arr_debug[$int_stack]['type']) ? $arr_debug[$int_stack]['type'] : '';
		
		if(isset($arr_debug[$int_stack]['object'])) {
			$str_class = get_class($arr_debug[$int_stack]['object']);
		} else if(isset($arr_debug[$int_stack]['class'])) {
			$str_class = $arr_debug[$int_stack]['class'];
		}
		
		$str_operation = $str_method . '()';
		if(!empty($str_class)) {
			$str_operation = $str_class . $str_type . $str_operation;
		}
	}
	
	$str_debug =  $str_file . ' [line ' . $str_line . ']';
	if(!empty($str_operation)) {
		$str_debug .= ' within '. $str_operation;
	}

	if(php_sapi_name() != 'cli') { 
		echo '<div style="font-weight: bold; font-size: 130%;">' . $str_title . ' <span style="font-weight: normal; font-size: 70%;">' . debug_datatype($mix) .'</span></div>';
		echo '<div style="font-size: 80%">' . $str_debug . '</div>';
	    debug_object($mix);
	} else {
		echo("\n");
        echo $str_title . '  ' . debug_datatype($mix) . "\n";
		echo $str_debug . "\n";
	    debug_object($mix);
        echo("\n");
	}
}

function kill($mix = null) {
    egg($mix, 'DIE', 1);
    die;
}

function asdf($mix = null) {
    if(php_sapi_name() != 'cli') { echo('<pre style="margin: 0; padding: 0;">'); }
	print_r($mix);
	if(php_sapi_name() != 'cli') { echo('</pre>'); }
	if(php_sapi_name() == 'cli') { echo("\n"); }
}

function trac($str) {
    syslog(LOG_CRIT, time() . ' -> ' . $str);
}
